package edu.unf.cen6940.web;

import edu.unf.cen6940.dao.FacultyresearchtostudentDAO;
import edu.unf.cen6940.dao.StudentDAO;
import edu.unf.cen6940.dao.ThesisDAO;

import edu.unf.cen6940.domain.Facultyresearchtostudent;
import edu.unf.cen6940.domain.Student;
import edu.unf.cen6940.domain.Thesis;

import edu.unf.cen6940.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Student entities
 * 
 */

@Controller("StudentController")
public class StudentController {

	/**
	 * DAO injected by Spring that manages Facultyresearchtostudent entities
	 * 
	 */
	@Autowired
	private FacultyresearchtostudentDAO facultyresearchtostudentDAO;

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Student entities
	 * 
	 */
	@Autowired
	private StudentService studentService;

	/**
	 * Show all Facultyresearchtostudent entities by Student
	 * 
	 */
	@RequestMapping("/listStudentFacultyresearchtostudents")
	public ModelAndView listStudentFacultyresearchtostudents(@RequestParam Integer studentIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(studentIdKey));
		mav.setViewName("student/facultyresearchtostudents/listFacultyresearchtostudents");

		return mav;
	}

	/**
	 * Save an existing Facultyresearchtostudent entity
	 * 
	 */
	@RequestMapping("/saveStudentFacultyresearchtostudents")
	public ModelAndView saveStudentFacultyresearchtostudents(@RequestParam Integer student_studentId, @ModelAttribute Facultyresearchtostudent facultyresearchtostudents) {
		Student parent_student = studentService.saveStudentFacultyresearchtostudents(student_studentId, facultyresearchtostudents);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("student", parent_student);
		mav.setViewName("student/viewStudent");

		return mav;
	}

	/**
	 * Entry point to show all Student entities
	 * 
	 */
	public String indexStudent() {
		return "redirect:/indexStudent";
	}

	/**
	 * Create a new Thesis entity
	 * 
	 */
	@RequestMapping("/newStudentThesises")
	public ModelAndView newStudentThesises(@RequestParam Integer student_studentId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("thesis", new Thesis());
		mav.addObject("newFlag", true);
		mav.setViewName("student/thesises/editThesises");

		return mav;
	}

	/**
	 * Select the child Thesis entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStudentThesises")
	public ModelAndView confirmDeleteStudentThesises(@RequestParam Integer student_studentId, @RequestParam Integer related_thesises_thesisId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId));
		mav.addObject("student_studentId", student_studentId);
		mav.setViewName("student/thesises/deleteThesises");

		return mav;
	}

	/**
	 * Create a new Student entity
	 * 
	 */
	@RequestMapping("/newStudent")
	public ModelAndView newStudent() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", new Student());
		mav.addObject("newFlag", true);
		mav.setViewName("student/editStudent");

		return mav;
	}

	/**
	 * Edit an existing Thesis entity
	 * 
	 */
	@RequestMapping("/editStudentThesises")
	public ModelAndView editStudentThesises(@RequestParam Integer student_studentId, @RequestParam Integer thesises_thesisId) {
		Thesis thesis = thesisDAO.findThesisByPrimaryKey(thesises_thesisId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("thesis", thesis);
		mav.setViewName("student/thesises/editThesises");

		return mav;
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@RequestMapping("/saveStudentThesises")
	public ModelAndView saveStudentThesises(@RequestParam Integer student_studentId, @ModelAttribute Thesis thesises) {
		Student parent_student = studentService.saveStudentThesises(student_studentId, thesises);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("student", parent_student);
		mav.setViewName("student/viewStudent");

		return mav;
	}

	/**
	 * View an existing Facultyresearchtostudent entity
	 * 
	 */
	@RequestMapping("/selectStudentFacultyresearchtostudents")
	public ModelAndView selectStudentFacultyresearchtostudents(@RequestParam Integer student_studentId, @RequestParam Integer facultyresearchtostudents_facultyResearchId, @RequestParam Integer facultyresearchtostudents_studentId, @RequestParam Integer facultyresearchtostudents_facultyId) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudents_facultyResearchId, facultyresearchtostudents_studentId, facultyresearchtostudents_facultyId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("facultyresearchtostudent", facultyresearchtostudent);
		mav.setViewName("student/facultyresearchtostudents/viewFacultyresearchtostudents");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Edit an existing Student entity
	 * 
	 */
	@RequestMapping("/editStudent")
	public ModelAndView editStudent(@RequestParam Integer studentIdKey, Model model) {
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("student", studentDAO.findStudentByPrimaryKey(studentIdKey));
		mav.setViewName("student/editStudent");
		
		return mav;
	}

	/**
	 * Select the child Facultyresearchtostudent entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStudentFacultyresearchtostudents")
	public ModelAndView confirmDeleteStudentFacultyresearchtostudents(@RequestParam Integer student_studentId, @RequestParam Integer related_facultyresearchtostudents_facultyResearchId, @RequestParam Integer related_facultyresearchtostudents_studentId, @RequestParam Integer related_facultyresearchtostudents_facultyId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("facultyresearchtostudent", facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents_facultyResearchId, related_facultyresearchtostudents_studentId, related_facultyresearchtostudents_facultyId));
		mav.addObject("student_studentId", student_studentId);
		mav.setViewName("student/facultyresearchtostudents/deleteFacultyresearchtostudents");

		return mav;
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@RequestMapping("/saveStudent")
	public String saveStudent(@ModelAttribute Student student) {
		studentService.saveStudent(student);
		return "forward:indexStudent";
	}

	/**
	 * Delete an existing Facultyresearchtostudent entity
	 * 
	 */
	@RequestMapping("/deleteStudentFacultyresearchtostudents")
	public ModelAndView deleteStudentFacultyresearchtostudents(@RequestParam Integer student_studentId, @RequestParam Integer related_facultyresearchtostudents_facultyResearchId, @RequestParam Integer related_facultyresearchtostudents_studentId, @RequestParam Integer related_facultyresearchtostudents_facultyId) {
		ModelAndView mav = new ModelAndView();

		Student student = studentService.deleteStudentFacultyresearchtostudents(student_studentId, related_facultyresearchtostudents_facultyResearchId, related_facultyresearchtostudents_studentId, related_facultyresearchtostudents_facultyId);

		mav.addObject("student_studentId", student_studentId);
		mav.addObject("student", student);
		mav.setViewName("student/viewStudent");

		return mav;
	}

	/**
	 */
	@RequestMapping("/studentController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Edit an existing Facultyresearchtostudent entity
	 * 
	 */
	@RequestMapping("/editStudentFacultyresearchtostudents")
	public ModelAndView editStudentFacultyresearchtostudents(@RequestParam Integer student_studentId, @RequestParam Integer facultyresearchtostudents_facultyResearchId, @RequestParam Integer facultyresearchtostudents_studentId, @RequestParam Integer facultyresearchtostudents_facultyId) {
		Facultyresearchtostudent facultyresearchtostudent = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(facultyresearchtostudents_facultyResearchId, facultyresearchtostudents_studentId, facultyresearchtostudents_facultyId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("facultyresearchtostudent", facultyresearchtostudent);
		mav.setViewName("student/facultyresearchtostudents/editFacultyresearchtostudents");

		return mav;
	}

	/**
	 * Show all Thesis entities by Student
	 * 
	 */
	@RequestMapping("/listStudentThesises")
	public ModelAndView listStudentThesises(@RequestParam Integer studentIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(studentIdKey));
		mav.setViewName("student/thesises/listThesises");

		return mav;
	}

	/**
	 * Select an existing Student entity
	 * 
	 */
	@RequestMapping("/selectStudent")
	public ModelAndView selectStudent(@RequestParam Integer studentIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(studentIdKey));
		mav.setViewName("student/viewStudent");

		return mav;
	}
	
	/**
	 * Select an existing Student entity
	 * 
	 */
	@RequestMapping("/viewStudent")
	public void adminSelectStudent(@RequestParam Integer id, Model model) {

		model.addAttribute("student", studentDAO.findStudentByPrimaryKey(id));
	}

	/**
	 * Select the Student entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStudent")
	public ModelAndView confirmDeleteStudent(@RequestParam Integer studentIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(studentIdKey));
		mav.setViewName("student/deleteStudent");

		return mav;
	}

	/**
	 * Show all Student entities
	 * 
	 */
	@RequestMapping("/indexStudent")
	public ModelAndView listStudents() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("students", studentService.loadStudents());

		mav.setViewName("student/listStudents");

		return mav;
	}

	/**
	 * View an existing Thesis entity
	 * 
	 */
	@RequestMapping("/selectStudentThesises")
	public ModelAndView selectStudentThesises(@RequestParam Integer student_studentId, @RequestParam Integer thesises_thesisId) {
		Thesis thesis = thesisDAO.findThesisByPrimaryKey(thesises_thesisId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("thesis", thesis);
		mav.setViewName("student/thesises/viewThesises");

		return mav;
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@RequestMapping("/deleteStudentThesises")
	public ModelAndView deleteStudentThesises(@RequestParam Integer student_studentId, @RequestParam Integer related_thesises_thesisId) {
		ModelAndView mav = new ModelAndView();

		Student student = studentService.deleteStudentThesises(student_studentId, related_thesises_thesisId);

		mav.addObject("student_studentId", student_studentId);
		mav.addObject("student", student);
		mav.setViewName("student/viewStudent");

		return mav;
	}

	/**
	 * Create a new Facultyresearchtostudent entity
	 * 
	 */
	@RequestMapping("/newStudentFacultyresearchtostudents")
	public ModelAndView newStudentFacultyresearchtostudents(@RequestParam Integer student_studentId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("student_studentId", student_studentId);
		mav.addObject("facultyresearchtostudent", new Facultyresearchtostudent());
		mav.addObject("newFlag", true);
		mav.setViewName("student/facultyresearchtostudents/editFacultyresearchtostudents");

		return mav;
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@RequestMapping("/deleteStudent")
	public String deleteStudent(@RequestParam Integer studentIdKey) {
		Student student = studentDAO.findStudentByPrimaryKey(studentIdKey);
		studentService.deleteStudent(student);
		return "forward:/indexStudent";
	}
	
	@RequestMapping("/searchStudent")
	public String searchStudent(@RequestParam String searchTerm, Model model){
		model.addAttribute("students", studentDAO.searchByTerm(searchTerm));
		return "student/listStudents";
	}
}